﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.ComponentModel;

namespace ImageCropper
{
    [Serializable, TypeConverter(typeof(GenericTypeConverter<Ratio>))]
    public class Ratio
    {
        private int _x;
        public int x
        {
            get
            {
                return this._x;
            }
            set
            {
                this._x = value;
            }
        }

        private int _y;
        public int y
        {
            get
            {
                return this._y;
            }
            set
            {
                this._y = value;
            }
        }

        public Ratio(int x, int y)
        {
            this._x = x;
            this._y = y;
        }

        static Ratio()
        {
            Empty = new Ratio(0, 0);
        }

        public static readonly Ratio Empty;
    }

    [Serializable, TypeConverter(typeof(GenericTypeConverter<Rectangle>))]
    public class Rectangle
    {
        private int _x1;
        public int x1
        {
            get
            {
                return this._x1;
            }
            set
            {
                this._x1 = value;
            }
        }

        private int _x2;
        public int x2
        {
            get
            {
                return this._x2;
            }
            set
            {
                this._x2 = value;
            }
        }

        private int _y1;
        public int y1
        {
            get
            {
                return this._y1;
            }
            set
            {
                this._y1 = value;
            }
        }
        private int _y2;
        public int y2
        {
            get
            {
                return this._y2;
            }
            set
            {
                this._y2 = value;
            }
        }

        public Rectangle(int x1, int x2, int y1,int y2)
        {
            this._x1 = x1;
            this._x2 = x2;
            this._y1 = y1;
            this._y2 = y2;
        }

        static Rectangle()
        {
            Empty = new Rectangle(0, 0, 0, 0);
        }

        public static readonly Rectangle Empty;
    }

    public class GenericTypeConverter<T> : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string s = jss.Serialize(value);
            return s;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                T t = jss.Deserialize<T>((string)value);
                return t;
            }
            else return base.ConvertFrom(context, culture, value);
        }
    }
}
